package com.fick.struts.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.VodServerDAO;
import com.fick.hibernate.model.VodServer;
import com.fick.tools.SystemLogger;
/**
 * 服务器管理
 * add by 王浩强
 * 2009-08-18
 * */
public class VodServerAction extends DispatchAction {

	private VodServerDAO dao=new VodServerDAO();
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		String path=request.getParameter("path");
		if (id!=null && id.trim().length()>0) {
			//修改操作
			VodServer vodServer=dao.findById(Integer.parseInt(id));
			if (vodServer!=null) {
				vodServer.setName(name);
				vodServer.setOnlinePath(path);
				dao.update(vodServer);
			}
		}else {
			//添加操作
			VodServer vodServer=new VodServer();
			vodServer.setName(name);
			vodServer.setOnlinePath(path);
			dao.save(vodServer);
		}
		response.sendRedirect("/admin/vodServer.jsp");
		return null;
	}

	public ActionForward delById(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String id=request.getParameter("id");
		if (id!=null && id.trim().length()>0) {
			VodServer vodServer=dao.findById(Integer.parseInt(id));
			if (vodServer!=null) {
				dao.delete(vodServer);
			}
		}
		return mapping.findForward("show");
	}
	public ActionForward delAll(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String str = request.getParameter("str");
		VodServer vodServer;
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				vodServer = dao.findById(id);
				if(vodServer!=null){
					dao.delete(vodServer);
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error(e);
		}
		return mapping.findForward("show");
	}
}
